const util = require("../../utils/util");

// pages/orderedit/orderedit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rid: "",
    beginTime: "",
    endTime: "",
    hotelName: "",
    roomName: "",
    numbers: 1,
    allPrice: "00.00",
    cpid: "",
    details: [],
    dialogflag: false,
    view1: false,
    view2: true,
    //可用优惠券
    canCouponList: [],
    //不可用优惠券
    unCanCouponList: []
  },

   /**
   * 点击可用
   */
  btn1(){
    this.setData({
      view1: false,
      view2: true
    });
  },

  /**
   * 点击不可用
   */
  btn2(){
    this.setData({
      view1: true,
      view2: false
    });
  },

  /**
   * 选择优惠券
   */
  selectcoupon(){

    //发送ajax
    util.myajax({
      method: "POST",
      url: util.url.couponUserListByOrders,
      data: {
        rid: this.data.rid,
        beginTime: this.data.beginTime,
        endTime: this.data.endTime,
        numbers: this.data.numbers == "" ? 1 : this.data.numbers
      },
      success: resp => {
        //打开弹窗
        this.setData({
          dialogflag: true,
          canCouponList: resp[0],
          unCanCouponList: resp[1]
        });
      }
    });
  },

  /**
   * 使用优惠券
   */
  useCoupon(options){
    // console.log(options.currentTarget.dataset.cpid);
    this.setData({
      cpid: options.currentTarget.dataset.cpid,
      dialogflag: false
    });

    //重新发起价格计算请求
    this.getOrderPrice();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      rid: options.rid,
      beginTime: options.beginTime,
      endTime: options.endTime,
      hotelName: options.hotelName,
      roomName: options.roomName
    });

    //获取订单的价格明细数据（总价 每天的价格列表）
    this.getOrderPrice();
  },

  //获取订单的价格明细
  getOrderPrice(){
    //发送ajax请求
    util.myajax({
      method: "POST",
      url: util.url.orderprices,
      data: {
        rid: this.data.rid,
        beginTime: this.data.beginTime,
        endTime: this.data.endTime,
        numbers: this.data.numbers == "" ? 1 : this.data.numbers,
        cpid: this.data.cpid
      },
      success: resp => {
        console.log(resp);
        this.setData({
          allPrice: resp.allprice,
          details: resp.priceDetails
        });
      }
    });
  },

  //输入框修改事件监听
  numberUpate(options){
    console.log(options);
    //重新请求获取最新的价格明细
    this.getOrderPrice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})